package com.stephenone.handlerdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private TextView tvHello;
    private ImageView imageView;
    private Button btn_stop;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            tvHello.setText("" + msg.arg1);
            tvHello.setText(msg.obj.toString());
        }
    };

    private Handler handler1 = new Handler(new Handler.Callback() {
        //表示是否消费当前message(是否不调用重写的handleMessage方法)
        @Override
        public boolean handleMessage(Message msg) {
            Log.e(TAG, "handleMessage: ");
            Toast.makeText(MainActivity.this,"callback",Toast.LENGTH_SHORT).show();
            return false;
        }
    }){
        @Override
        public void handleMessage(Message msg) {
            Log.e(TAG, "handleMessage: " + "Override");
            Toast.makeText(MainActivity.this,"Override handleMessage",Toast.LENGTH_SHORT).show();
        }
    };
    private int[] images = {R.drawable.p1,R.drawable.p2,R.drawable.p3};
    private int index;
    private MyRunnable myRunnable = new MyRunnable();

    class MyRunnable implements Runnable {
        @Override
        public void run() {
            index ++ ;
            index %= 3;
            Log.e(TAG, "run: "+ index);
            imageView.setImageResource(images[index]);
            handler.postDelayed(myRunnable,1000);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvHello = (TextView) findViewById(R.id.tv_hello);
        imageView = (ImageView) findViewById(R.id.iv_img);
        btn_stop = (Button)findViewById(R.id.btn_stop);

        //handler.post形式
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
////                tvHello.setText("fuck"); //不能在非ui线程更新ui
//
//                handler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        //Runnable在ui线程执行
//                        tvHello.setText("fuck");
//                    }
//                });
//            }
//        }).start();
//



        handler.postDelayed(myRunnable,1000);
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //移除所有MessageQueue中的myRunnable
                handler.removeCallbacks(myRunnable);
            }
        });


//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                Message message = new Message();
//                message.arg1 = 30624700;
//                handler.sendMessage(message);
//            }
//        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                Message message = new Message();
//                message.obj = new Person("One",18);
                Message message = handler.obtainMessage();
                message.obj = new Person("One",18);
//                handler.sendMessage(message);
                // handler.obtainMessage()得到一个target为当前handler的Message对象，
                // 可以通过message.sendToTarget来发送消息到handler。（内部实现是target.sendMessage）
                message.sendToTarget();
            }
        }).start();

        handler1.sendEmptyMessage(111);

    }

    class Person {
        public Person(String name,int age){
            this.name = name;
            this.age = age;
        }
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}
